<?php

use Illuminate\Database\Seeder;
use Laravel\Passport\Client;
use Carbon\Carbon;
use Illuminate\Support\Facades\Artisan;


class LaravelAdminTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $userModelName =  config('admin.database.users_model');
        $userModel = new $userModelName;
        $userModel::query()->truncate();
        $userModel::query()->create([
            'username' => 'admin',
            'password' => bcrypt('123456'),
            'name'     => '管理员',
            'key' => \Ramsey\Uuid\Uuid::uuid4()
        ]);
        // create a user.


        // add default menus.
        $menuModelName =  config('admin.database.menu_model');
        $menuModel = new $menuModelName;
        $menuModel::query()->truncate();

        $menus = [
            [
                'title'     => '用户',
                'icon'      => 'fa fa-users',
                'uri'       => 'user',
            ],
            [
                'title'     => '版块',
                'icon'      => 'fa fa-table',
                'uri'       => 'forum',
            ],
            [
                'title'     => '版块分类',
                'icon'      => 'fa fa-list-ul',
                'uri'       => 'forum-taxonomy',
            ],
            [
                'title'     => '帖子',
                'icon'      => 'fa fa-forumbee',
                'uri'       => 'topic',
            ],
            [
                'title'     => '置顶帖',
                'icon'      => 'fa fa-forumbee',
                'uri'       => 'sticky-topic',
            ],
            [
                'title'     => '回复',
                'icon'      => 'fa fa-commenting-o',
                'uri'       => 'comment',
            ],
            [
                'title'     => '轮播',
                'icon'      => 'fa fa-commenting-o',
                'uri'       => 'swiper',
            ],
            [
                'title'     => '标签',
                'icon'      => 'fa fa-commenting-o',
                'uri'       => 'tag',
            ],
            [
                'title'     => '标签分类',
                'icon'      => 'fa fa-commenting-o',
                'uri'       => 'tag-taxonomy',
            ],
            [
                'title'     => '附件',
                'icon'      => 'fa fa-file',
                'uri'       => 'attachment',
            ],
            [
                'title'     => '系统',
                'icon'      => 'fa-tasks',
                'uri'       => '',
                'children' => [
                    [
                        'title'     => '管理员',
                        'icon'      => 'fa fa-users',
                        'uri'       => 'auth/users',
                    ],
                    [
                        'title'     => '菜单',
                        'icon'      => 'fa fa-bars',
                        'uri'       => 'auth/menu',
                    ],
                    [
                        'title'     => '工具',
                        'icon'      => 'fas fa-tools',
                        'uri'       => 'tool',
                    ],
                ]
            ],

        ];

        $this->insertMenu($menuModel, $menus);


        if (Client::count() == 0)
        {
            Artisan::call('passport:install');
        }
        if (config('telescope.enabled')) {
            Artisan::call('telescope:install');
            Artisan::call('migrate');
        }

    }

    private function insertMenu($menuModel, $menus, $parentId = 0)
    {
        foreach ($menus as $key => $value)
        {
            $menuModel::query()->insert([
                'parent_id' => $parentId,
                'order' => $key + 1,
                'title' => $value['title'],
                'icon' => $value['icon'],
                'uri' => $value['uri'],
                'created_at' => Carbon::now()->toDateTimeString(),
                'updated_at' => Carbon::now()->toDateTimeString(),
            ]);
            if (!empty($value['children']))
            {
                $this->insertMenu($menuModel, $value['children'], \DB::getPdo()->lastInsertId());
            }
        }
    }
}
